package com.geek.dubbo.study.provider;

import com.alibaba.dubbo.rpc.RpcContext;
import com.geek.dubbo.study.common.DemoService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

public class DemoServiceImpl implements DemoService {
  @Override
  public String sayHello(String name) {
    try {
      TimeUnit.MILLISECONDS.sleep(50 + ThreadLocalRandom.current().nextInt(150));
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + com.alibaba.dubbo.rpc.RpcContext.getContext().getRemoteAddress());
    return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
  }

}
